package com.justgame.project.cloud.gateway.filter;

import com.alibaba.fastjson.JSONObject;
import com.justgame.project.cloud.common.constant.Result;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 响应体 Response Body
 * @author ZhouWeiBo
 * @create 2022/6/15
 */
@Component
public class ResponseBodyFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //ServerHttpResponse originalResponse = exchange.getResponse();
        //ServerHttpRequest request = exchange.getRequest();
        ////Content-Type
        //MediaType contentType = originalResponse.getHeaders().getContentType();
        //DataBufferFactory bufferFactory = originalResponse.bufferFactory();
        //ServerHttpResponseDecorator decoratedResponse = new ServerHttpResponseDecorator(originalResponse) {
        //    @Override
        //    public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
        //        /* 排除输出流媒体 */
        //        if (body instanceof Flux &&
        //                (contentType == null || !contentType.equals(MediaType.APPLICATION_OCTET_STREAM))
        //        ) {
        //            Flux<? extends DataBuffer> fluxBody = (Flux<? extends DataBuffer>) body;
        //            return super.writeWith(fluxBody.map(dataBuffer -> {
        //                // probably should reuse buffers
        //                byte[] content = new byte[dataBuffer.readableByteCount()];
        //                dataBuffer.read(content);
        //                //释放掉内存
        //                DataBufferUtils.release(dataBuffer);
        //
        //                Result<?> result = JSONObject.parseObject(content, Result.class);
        //
        //                /* 读取ResponseBody只是为了存储Log */
        //                return bufferFactory.wrap(content);
        //            }));
        //        }
        //        // if body is not a flux. never got there.
        //        return super.writeWith(body);
        //    }
        //};
        //// replace response with decorator
        //return chain.filter(exchange.mutate().response(decoratedResponse).build());
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return -2;
    }
}
